//
//  MainView.m
//  SlashEM
//
//  Created by dirk on 12/30/09.
//  Copyright 2009 Dirk Zimmermann. All rights reserved.
//

//  This file is part of Slash'EM.
//
//  Slash'EM is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, version 2 of the License only.
//
//  Slash'EM is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with Slash'EM.  If not, see <http://www.gnu.org/licenses/>.

// device font families
// + Arial Unicode MS
// + Heiti K
// + DB LCD Temp
// + Helvetica
// + Marker Felt
// + Times New Roman
// + Verdana
// + Georgia
// + Arial Rounded MT Bold
// + Trebuchet MS
// + Heiti TC
// + Geeza Pro
// + Courier
// + Arial
// + Heiti J
// + Arial Hebrew
// + Courier New
// + Zapfino
// + American Typewriter
// + Heiti SC
// + Helvetica Neue
// + Thonburi

#import "MainView.h"
#import "NhWindow.h"
#import "NhMapWindow.h"
#import "MainViewController.h"
#import "UIFactory.h"
#import "ActionBar.h"
#import "TileSet.h"
#import "MapView.h"
#import "WindowMessageView.h"

extern const char *hu_abbrev_stat[];

@implementation MainView

@synthesize mapView;

- (void)awakeFromNib {
	[super awakeFromNib];
	
	self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
	self.contentMode = UIViewContentModeRedraw;
	self.clearsContextBeforeDrawing = YES;
	
	// status label 2
	int numberOfLines = 1;
	CGRect labelFrame = self.bounds;
	labelFrame.size.height = 14.0f * numberOfLines;
	labelFrame.origin.y = self.bounds.size.height - [UIFactory buttonHeight] - labelFrame.size.height;
	statusView2 = [UIFactory messageLabelWithFrame:labelFrame numberOfLines:numberOfLines];
	statusView2.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
	[self addSubview:statusView2];
	
	// status label 1
	numberOfLines = 1;
	labelFrame = statusView2.frame;
	labelFrame.size.height = 14.0f * numberOfLines;
	labelFrame.origin.y -= labelFrame.size.height;
	statusView1 = [UIFactory messageLabelWithFrame:labelFrame numberOfLines:numberOfLines];
	statusView1.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
	[self addSubview:statusView1];
	
	// message view
	labelFrame.origin.y = 0.0f;
	numberOfLines = 3;
	labelFrame.size.height = 14.0f * numberOfLines;
	messageView = [[WindowMessageView alloc] initWithFrame:labelFrame];
	messageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
	messageView.userInteractionEnabled = YES;
	messageView.editable = NO;
	messageView.textColor = [UIFactory textColor];
	messageView.backgroundColor = [UIFactory backgroundColor];
	[self addSubview:messageView];
	[messageView release];

	// map view
	float yStart = messageView.frame.origin.y+messageView.frame.size.height;
	mapView = [[MapView alloc] initWithFrame:CGRectMake(0.0f, yStart,
														self.bounds.size.width,
														statusView1.frame.origin.y-yStart)];
	mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
	mapView.contentMode = UIViewContentModeRedraw;
	mapView.backgroundColor = [UIFactory mapBackgroundColor];
	[self addSubview:mapView];
	[mapView release];
	
	[[MainViewController instance] viewIsSetup];
}

- (BOOL) isMessageView:(UIView *)v {
	return v == messageView;
}

- (BOOL)isStatusView:(UIView *)v {
	return v == statusView1;
}

- (CGRect)bigSubviewFrame {
	CGRect r = CGRectInset(self.bounds, 20.0f, 10.0f);
	r.origin.y = messageView.frame.origin.y+messageView.bounds.size.height;
	r.size.height = self.bounds.size.height-r.origin.y-statusView1.bounds.size.height;
	return r;
}

- (CGRect)textInputFrame {
	float marginTop = 10.0f;
	float insetX = 20.0f;
	return CGRectMake(insetX/2, messageView.frame.origin.y+messageView.bounds.size.height+marginTop,
					  self.bounds.size.width-insetX, 26.0f);
}

- (CGRect)actionBarFrame {
	CGRect frame = self.bounds;
	frame.size.height = [UIFactory buttonHeight];
	frame.origin.y = self.bounds.size.height-frame.size.height;
	return frame;
}

- (void)refreshMessages {
	messageView.nhWindow = [NhWindow messageWindow];
	NSString *text = [[NhWindow messageWindow] text];
	if (!text || text.length == 0) {
		messageView.hidden = YES;
	} else {
		messageView.text = text;
		messageView.hidden = NO;
		CGSize contentSize = messageView.contentSize;
		float textHeight = contentSize.height;
		float messageViewHeight = messageView.bounds.size.height;
		float scrollToY = textHeight-messageViewHeight;
		scrollToY = scrollToY < 0.0f ? 0.0f : scrollToY;
		[messageView setContentOffset:CGPointMake(0.0f, scrollToY) animated:YES];
	}
	if ([NhWindow statusWindow]) {
		NSArray *messages = [NhWindow statusWindow].messages;
		if (messages.count == 2) {
			statusView1.text = [messages objectAtIndex:0];
			statusView2.text = [messages objectAtIndex:1];
		}
	}
}

- (void)dealloc {
    [super dealloc];
}

@end
